package com.hundsun.s3.platform.backstage.action.client;

import com.hundsun.s3.common.dto.ClientPageDTO;
import com.hundsun.s3.common.dto.DeptDTO;
import com.hundsun.s3.common.dto.Message;
import com.hundsun.s3.common.dto.OrderDTO;
import com.hundsun.s3.common.util.DictDept;
import com.hundsun.s3.platform.service.BackClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * com.hundsun.s3.platform.backstage.action.client
 *
 * @Description:后台客户管理Action
 * @Author: zhanglei23613
 * @Date: Created in 13:56 2018/2/9 0009
 * V1.0 13:56 2018/2/9 0009
 */
@Controller
public class ClientManageAction {
    @Autowired
    private BackClientService clientService;

    @RequestMapping(value = "/back/clientManage/getClientData.json")
    public @ResponseBody
    Map getTableData(Integer pageNo, Integer pageSize, String username, String nickname, String mailbox, String department, String phonenumber) throws IOException {
        /**
         * @Description:模糊查询分页显示
         * @param: [pageNo, pageSize]
         * @return: java.util.Map
         * @Author: zhanglei23613
         * @Date: 16:47 2018/2/27 0027
         * V1.0
         */
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map> resultData = new ArrayList<Map>();
        List<DeptDTO> deptList=clientService.getDeptList();
        DictDept dictDept = DictDept.build(deptList);
        List<ClientPageDTO> clientList = clientService.findPage(pageNo, pageSize, username, nickname, mailbox, phonenumber, department);
        for (int i = 0; i < clientList.size(); i++) {
            Map map = new HashMap();
            map.put("username", clientList.get(i).getUsername());
            map.put("nickname", clientList.get(i).getNickname());
            map.put("sex", clientList.get(i).getSex());
            map.put("birthday", clientList.get(i).getBirthday());
            map.put("mailbox", clientList.get(i).getMailbox());
            map.put("phonenumber", clientList.get(i).getPhonenumber());
            map.put("departmentId", clientList.get(i).getDept());
            map.put("department",dictDept.getValue(clientList.get(i).getDept()));
            map.put("create_date", clientList.get(i).getCreate_date());
            map.put("status", clientList.get(i).getStatus());
            resultData.add(map);
        }

        int total = clientService.getTotal(username, nickname, mailbox, phonenumber, department);
        result.put("rows", resultData);
        result.put("total", total);
        return result;
    }

    @RequestMapping(value = "/back/clientManage/addClientData.json")
    public @ResponseBody
    Map<String, Object> addClientData(String username_add, String password_add, int sex_add, String nickname_add, String mailbox_add, String birthday_add, String phonenumber_add, String department_add) {
        /**
         * @Description:增加用户
         * @param: [username_add, password_add, sex_add, nickname_add, mailbox_add, birthday_add, phonenumber_add, department_add, group_add]
         * @return: java.util.Map<java.lang.String , java.lang.Object>
         * @Author: zhanglei23613
         * @Date: 9:33 2018/3/8 0008
         * V1.0
         */

        Map<String, Object> msg = new HashMap<String, Object>();
        String result = clientService.register(username_add, password_add, sex_add, nickname_add, mailbox_add, birthday_add, phonenumber_add, department_add);
        msg.put("msg", result);
        return msg;
    }

    @RequestMapping(value = "/back/clientManage/logoutClient.json")
    public @ResponseBody
    Map<String, Object> doLogoutClient(String username) {
        /**
         * @Description:注销用户
         * @param: [username]
         * @return: java.util.Map<java.lang.String , java.lang.Object>
         * @Author: zhanglei23613
         * @Date: 17:48 2018/3/8 0008
         * V1.0
         */

        Map<String, Object> msg = new HashMap<String, Object>();
        String[] result= new String[10];
        String[] usernames=username.split(",");
        for(int i=0;i<usernames.length;i++){
            result[i]=clientService.cancle(usernames[i]);
            msg.put(usernames[i],result[i]);
        }
        return msg;
    }

    @RequestMapping(value = "/back/clientManage/activateClient.json")
    public @ResponseBody
    Map<String, Object> doactivateClient(String username) {
        /**
         * @Description:激活用户
         * @param: [username]
         * @return: java.util.Map<java.lang.String , java.lang.Object>
         * @Author: zhanglei23613
         * @Date: 17:49 2018/3/8 0008
         * V1.0
         */

        Map<String, Object> msg = new HashMap<String, Object>();
        String[] result= new String[10];
        String[] usernames=username.split(",");
        for(int i=0;i<usernames.length;i++){
            result[i]=clientService.cancleReopen(usernames[i]);
            msg.put(usernames[i],result[i]);
        }
        return msg;
    }

    @RequestMapping(value = "/back/clientManage/modifyClient.json")
    public @ResponseBody
    Map<String, Object> domodifyClient(String username_mod, String nickname_mod, String phonenumber_mod, String department_mod) {
        /**
         * @Description:管理员修改客户信息
         * @param: [username_mod, nickname_mod, phonenumber_mod, department_mod]
         * @return: java.util.Map<java.lang.String,java.lang.Object>
         * @Author: zhanglei23613
         * @Date: 14:09 2018/3/12 0012
         * V1.0
         */

        Map<String, Object> msg = new HashMap<String, Object>();
        String result=clientService.updateUserInfo(username_mod,nickname_mod,phonenumber_mod,department_mod);
        msg.put("msg",result);
        return msg;
    }
    @RequestMapping(value = "/back/clientManage/getOrderByClientId.json")
    public @ResponseBody
    Map doGetOrderByClientId(Integer pageNo, Integer pageSize, String client_username, String server_start_time, String server_end_time) throws IOException {
        /**
         * @Description:根据客户账户名查询工单
         * @param: [pageNo, pageSize, client_username, server_start_time, server_end_time]
         * @return: java.util.Map
         * @Author: zhanglei23613
         * @Date: 18:57 2018/3/16 0016
         * V1.0
         */

        if(client_username==null||client_username.equals("")){
            client_username="";
        };
        long startTime=Long.parseLong(server_start_time);
        long endTime=Long.parseLong(server_end_time);
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map> resultData = new ArrayList<Map>();
        List<OrderDTO> orserList=clientService.getOrderByClientId(pageNo,pageSize,client_username,startTime,endTime);
        for (int i = 0; i < orserList.size(); i++) {
            Map map = new HashMap();
            map.put("orderId", orserList.get(i).getOrder_serial_id());
            map.put("clientId", orserList.get(i).getUsername());
            map.put("staffId", orserList.get(i).getStaff_id());
            map.put("serverStartTime", orserList.get(i).getServer_start_time());
            map.put("serverEndTime", orserList.get(i).getServer_end_time());
            map.put("clientName", orserList.get(i).getUsername());
            resultData.add(map);
        }

        int total = clientService.getOrderTotal(pageNo,pageSize,client_username,startTime,endTime);
        result.put("rows", resultData);
        result.put("total", total);
        return result;
    }

    @RequestMapping(value="/back/clientManage/getMessageByUsername.json")
    public @ResponseBody List<Message> doGetMessageByUsername(String params){
        String[] param= params.split(",");
        String client_id=param[0];
        String staff_id=param[1];
        long start_time=Long.parseLong(param[2]);
        long end_time=Long.parseLong(param[3]);
        List<Message> messageList=clientService.getMessageByUsername(client_id,staff_id,start_time,end_time);
        return messageList;
    }
}
